package com.cennavi.minemapdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;

public class SettingActivity extends Activity implements View.OnClickListener {

    private EditText mEtServerHost;
    private EditText mEtServerScheme;
    private EditText mEtMapID;
    private EditText mEtMapTok;
    private EditText mEtMapNightID;
    private EditText mEtMapNightTok;
    private EditText mEtSatelliteUrl;
    private EditText mEtOfflineUrl;
    private EditText mEtOfflineTok;
    private EditText mEtApiBaseUrl;
    private EditText mEtApiUrl;
    private EditText mEtSpriteGlyphsUrl;

    private Button mBtnGenerate;

    private int mExpandViewType = 0;
    private int mMapType = 0;
    private int mCoordType = 0;
    private int mLocationType = 0;

    private SharedPreferences mSharedPreferences;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_set);
        mContext = this;
        // 初始化UI控件
        initView();
    }

    private void initView() {
        mEtServerHost = findViewById(R.id.et_host_input);
        SpannableString sServerHost = new SpannableString(Config.HOST);
        mEtServerHost.setHint(sServerHost);

        mEtServerScheme = findViewById(R.id.et_scheme_input);
        SpannableString sServerScheme = new SpannableString(Config.SCHEME);
        mEtServerScheme.setHint(sServerScheme);

        mEtMapID = findViewById(R.id.et_basicmap_input);
        SpannableString sMapID = new SpannableString(Config.MAP_ID);
        mEtMapID.setHint(sMapID);

        mEtMapTok = findViewById(R.id.et_basicmap_appkey_input);
        SpannableString sMapTok = new SpannableString(Config.MAP_TOK);
        mEtMapTok.setHint(sMapTok);

        mEtMapNightID = findViewById(R.id.et_night_input);
        SpannableString sMapNightID = new SpannableString(Config.MAP_NIGHT_ID);
        mEtMapNightID.setHint(sMapNightID);

        mEtMapNightTok = findViewById(R.id.et_night_appkey_input);
        SpannableString sMapNightTok = new SpannableString(Config.MAP_NIGHT_TOK);
        mEtMapNightTok.setHint(sMapNightTok);

        mEtSatelliteUrl = findViewById(R.id.et_satellite_input);
        SpannableString sSatelliteUrl = new SpannableString(Config.SATELLITE_URL);
        mEtSatelliteUrl.setHint(sSatelliteUrl);

        mEtOfflineUrl = findViewById(R.id.et_offline_input);
        SpannableString sOfflineUrl = new SpannableString(Config.OFFLINE_URL);
        mEtOfflineUrl.setHint(sOfflineUrl);

        mEtOfflineTok = findViewById(R.id.et_offline_appkey_input);
        SpannableString sOfflineTok = new SpannableString(Config.OFFLINE_TOK);
        mEtOfflineTok.setHint(sOfflineTok);

        mEtApiBaseUrl = findViewById(R.id.et_minemap_apiBaseUrl_input);
        SpannableString sApiBaseUrl = new SpannableString(Config.API_BASE_URL);
        mEtApiBaseUrl.setHint(sApiBaseUrl);

        mEtApiUrl = findViewById(R.id.et_minemap_apiUrl_input);
        SpannableString sApiUrl = new SpannableString(Config.API_URL);
        mEtApiUrl.setHint(sApiUrl);

        mEtSpriteGlyphsUrl = findViewById(R.id.et_minemap_spriteGlyphsUrl_input);
        SpannableString sSpriteGlyphsUrl = new SpannableString(Config.SPRITE_GLYPHS_URL);
        mEtSpriteGlyphsUrl.setHint(sSpriteGlyphsUrl);

        mBtnGenerate = findViewById(R.id.btn_generate);
        mBtnGenerate.setOnClickListener(this);
    }

    private void setData() {
        SpannableString sServerHost = new SpannableString(Config.HOST);
        mEtServerHost.setHint(sServerHost);

        SpannableString sServerScheme = new SpannableString(Config.SCHEME);
        mEtServerScheme.setHint(sServerScheme);

        SpannableString sMapID = new SpannableString(Config.MAP_ID);
        mEtMapID.setHint(sMapID);

        SpannableString sMapTok = new SpannableString(Config.MAP_TOK);
        mEtMapTok.setHint(sMapTok);

        SpannableString sMapNightID = new SpannableString(Config.MAP_NIGHT_ID);
        mEtMapNightID.setHint(sMapNightID);

        SpannableString sMapNightTok = new SpannableString(Config.MAP_NIGHT_TOK);
        mEtMapNightTok.setHint(sMapNightTok);

        SpannableString sSatelliteUrl = new SpannableString(Config.SATELLITE_URL);
        mEtSatelliteUrl.setHint(sSatelliteUrl);

        SpannableString sOfflineUrl = new SpannableString(Config.OFFLINE_URL);
        mEtOfflineUrl.setHint(sOfflineUrl);

        SpannableString sOfflineTok = new SpannableString(Config.OFFLINE_TOK);
        mEtOfflineTok.setHint(sOfflineTok);

        SpannableString sApiBaseUrl = new SpannableString(Config.API_BASE_URL);
        mEtApiBaseUrl.setHint(sApiBaseUrl);

        SpannableString sApiUrl = new SpannableString(Config.API_URL);
        mEtApiUrl.setHint(sApiUrl);

        SpannableString sSpriteGlyphsUrl = new SpannableString(Config.SPRITE_GLYPHS_URL);
        mEtSpriteGlyphsUrl.setHint(sSpriteGlyphsUrl);
    }

    private void clearInput() {
        mEtServerHost.setText("");
        mEtServerScheme.setText("");
        mEtMapID.setText("");
        mEtMapTok.setText("");
        mEtMapNightID.setText("");
        mEtMapNightTok.setText("");
        mEtSatelliteUrl.setText("");
        mEtOfflineUrl.setText("");
        mEtOfflineTok.setText("");
        mEtApiBaseUrl.setText("");
        mEtApiUrl.setText("");
        mEtSpriteGlyphsUrl.setText("");
    }

    /**
     * @param context
     * @param key
     * @param values
     */
    private void putString(Context context, String key, String values) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit()
                .putString(key, values)
                .apply();
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private String getString(Context context, String key, String defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, defaultValues);
    }

    /**
     * @param context
     * @param key
     * @param values
     */
    private void putInt(Context context, String key, int values) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        mSharedPreferences.edit()
                .putInt(key, values)
                .apply();
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private int getInt(Context context, String key, int defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }

        return mSharedPreferences.getInt(key, defaultValues);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_generate:
                String serverHost = mEtServerHost.getText().toString();
                String serverScheme = mEtServerScheme.getText().toString();
                String mapID = mEtMapID.getText().toString();
                String mapTok = mEtMapTok.getText().toString();
                String mapNightID = mEtMapNightID.getText().toString();
                String mapNightTok = mEtMapNightTok.getText().toString();
                String satelliteUrl = mEtSatelliteUrl.getText().toString();
                String offlineUrl = mEtOfflineUrl.getText().toString();
                String offlineTok = mEtOfflineTok.getText().toString();
                String apiBaseUrl = mEtApiBaseUrl.getText().toString();
                String apiUrl = mEtApiUrl.getText().toString();
                String spriteGlyphsUrl = mEtSpriteGlyphsUrl.getText().toString();

                if (!TextUtils.isEmpty(serverHost)) {
                    Config.HOST = serverHost;
                    putString(mContext, "MineMap_serverHost", serverHost);
                }

                if (!TextUtils.isEmpty(serverScheme)) {
                    Config.SCHEME = serverScheme;
                    putString(mContext, "MineMap_serverScheme", serverScheme);
                }

                if (!TextUtils.isEmpty(mapID)) {
                    Config.MAP_ID = mapID;
                    putString(mContext, "MineMap_mapID", mapID);
                }

                if (!TextUtils.isEmpty(mapTok)) {
                    Config.MAP_TOK = mapTok;
                    putString(mContext, "MineMap_mapTok", mapTok);
                }

                if (!TextUtils.isEmpty(mapNightID)) {
                    Config.MAP_NIGHT_ID = mapNightID;
                    putString(mContext, "MineMap_mapNightID", mapNightID);
                }

                if (!TextUtils.isEmpty(mapNightTok)) {
                    Config.MAP_NIGHT_TOK = mapNightTok;
                    putString(mContext, "MineMap_mapNightTok", mapNightTok);
                }

                if (!TextUtils.isEmpty(serverScheme) && TextUtils.isEmpty(serverHost)) {
                    Config.BASE_URL = serverScheme + "://" + Config.HOST + "/service/solu/style/id/";
                } else if (TextUtils.isEmpty(serverScheme) && !TextUtils.isEmpty(serverHost)) {
                    Config.BASE_URL = Config.SCHEME + "://" + serverHost + "/service/solu/style/id/";
                } else if (TextUtils.isEmpty(serverScheme) && TextUtils.isEmpty(serverHost)) {
                    Config.BASE_URL = Config.SCHEME + "://" + Config.HOST + "/service/solu/style/id/";
                } else {
                    Config.BASE_URL = serverScheme + "://" + serverHost + "/service/solu/style/id/";
                }

                if (!TextUtils.isEmpty(satelliteUrl)) {
                    Config.SATELLITE_URL = satelliteUrl;
                    putString(mContext, "MineMap_satelliteUrl", satelliteUrl);
                }

                if (!TextUtils.isEmpty(offlineUrl)) {
                    Config.OFFLINE_URL = offlineUrl;
                    putString(mContext, "MineMap_offlineUrl", offlineUrl);
                }

                if (!TextUtils.isEmpty(offlineTok)) {
                    Config.OFFLINE_TOK = offlineTok;
                    putString(mContext, "MineMap_offlineTok", offlineTok);
                }

                if (!TextUtils.isEmpty(apiBaseUrl)) {
                    Config.API_BASE_URL = apiBaseUrl;
                    putString(mContext, "MineMap_apiBaseUrl", apiBaseUrl);
                }

                if (!TextUtils.isEmpty(apiUrl)) {
                    Config.API_URL = apiUrl;
                    putString(mContext, "MineMap_apiUrl", apiUrl);
                }

                if (!TextUtils.isEmpty(spriteGlyphsUrl)) {
                    Config.SPRITE_GLYPHS_URL = spriteGlyphsUrl;
                    putString(mContext, "MineMap_spriteGlyphsUrl", spriteGlyphsUrl);
                }

                clearInput();
                setData();

                Toast.makeText(SettingActivity.this, "配置成功！", Toast.LENGTH_LONG).show();
                break;
        }
    }
}
